added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CSASPNETMenu / CSASPNETMenu.aspx.cs
blob3c9dd0f61c40e6169860233f0ba7d147d5d43230
1 /****************************** Module Header ******************************\
2 * Module Name: CSASPNETMenu.aspx.cs
3 * Project: CSASPNETMenu
4 * Copyright (c) Microsoft Corporation.
6 * The CSASPNETMenu sample demonstrates how to bind a ASP.NET Menu Control
7 * to Database.
8 *
9 * All other rights reserved.
11 * History:
12 * * 8/27/2009 11:00 AM Zong-Qing Li Created
13 \***************************************************************************/
14 using System;
15 using System.Collections.Generic;
16 using System.Linq;
17 using System.Web;
18 using System.Web.UI;
19 using System.Web.UI.WebControls;
20 using System.Data;
22 namespace CSASPNETMenu
24 public partial class CSASPNETMenu : System.Web.UI.Page
26 protected void Page_Load(object sender, EventArgs e)
28 if (!IsPostBack)
30 GenerateMenuItem();
34 public void GenerateMenuItem()
36 // Get the data from database.
37 DataSet ds = GetData();
39 foreach (DataRow mainRow in ds.Tables[0].Rows)
41 // Load the records from the main table to the menu control.
42 MenuItem masterItem = new MenuItem(mainRow["mainName"].ToString());
43 masterItem.NavigateUrl = mainRow["mainUrl"].ToString();
44 Menu1.Items.Add(masterItem);
46 foreach (DataRow childRow in mainRow.GetChildRows("Child"))
48 // According to the relation of the main table and the child table, load the data from the child table.
49 MenuItem childItem = new MenuItem((string)childRow["childName"]);
50 childItem.NavigateUrl = childRow["childUrl"].ToString();
51 masterItem.ChildItems.Add(childItem);
56 public DataSet GetData()
58 // In order to test, we use the memory tables as the datasource.
59 DataTable mainTB = new DataTable();
60 DataColumn mainIdCol = new DataColumn("mainId");
61 DataColumn mainNameCol = new DataColumn("mainName");
62 DataColumn mainUrlCol = new DataColumn("mainUrl");
63 mainTB.Columns.Add(mainIdCol);
64 mainTB.Columns.Add(mainNameCol);
65 mainTB.Columns.Add(mainUrlCol);
67 DataTable childTB = new DataTable();
68 DataColumn childIdCol = new DataColumn("childId");
69 DataColumn childNameCol = new DataColumn("childName");
71 // The MainId column of the child table is the foreign key to the main table.
72 DataColumn childMainIdCol = new DataColumn("MainId");
73 DataColumn childUrlCol = new DataColumn("childUrl");
75 childTB.Columns.Add(childIdCol);
76 childTB.Columns.Add(childNameCol);
77 childTB.Columns.Add(childMainIdCol);
78 childTB.Columns.Add(childUrlCol);
81 // Insert some test records to the main table.
82 DataRow dr = mainTB.NewRow();
83 dr[0] = "1";
84 dr[1] = "Home";
85 dr[2] = "test.aspx";
86 mainTB.Rows.Add(dr);
87 DataRow dr1 = mainTB.NewRow();
88 dr1[0] = "2";
89 dr1[1] = "Articles";
90 dr1[2] = "test.aspx";
91 mainTB.Rows.Add(dr1);
92 DataRow dr2 = mainTB.NewRow();
93 dr2[0] = "3";
94 dr2[1] = "Help";
95 dr2[2] = "test.aspx";
96 mainTB.Rows.Add(dr2);
97 DataRow dr3 = mainTB.NewRow();
98 dr3[0] = "4";
99 dr3[1] = "DownLoad";
100 dr3[2] = "test.aspx";
101 mainTB.Rows.Add(dr3);
104 // Insert some test records to the child table
105 DataRow dr5 = childTB.NewRow();
106 dr5[0] = "1";
107 dr5[1] = "ASP.NET";
108 dr5[2] = "2";
109 dr5[3] = "test.aspx";
110 childTB.Rows.Add(dr5);
111 DataRow dr6 = childTB.NewRow();
112 dr6[0] = "2";
113 dr6[1] = "SQL Server";
114 dr6[2] = "2";
115 dr6[3] = "test.aspx";
116 childTB.Rows.Add(dr6);
117 DataRow dr7 = childTB.NewRow();
118 dr7[0] = "3";
119 dr7[1] = "JavaScript";
120 dr7[2] = "2";
121 dr7[3] = "test.aspx";
122 childTB.Rows.Add(dr7);
124 // Use the DataSet to contain that two tables.
125 DataSet ds = new DataSet();
126 ds.Tables.Add(mainTB);
127 ds.Tables.Add(childTB);
129 // Build the relation between the main table and the child table.
130 ds.Relations.Add("Child", ds.Tables[0].Columns["mainId"], ds.Tables[1].Columns["MainId"]);
133 return ds;